﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing.Printing;
using System.Drawing;
using TradeForm.Properties;
using System.Configuration;
using TradeForm.config;
using System.Xml.Linq;
using System.Xml;
using System.IO;
using System.Windows.Forms;

namespace TradeForm.Print
{
    public class PostalOrderDocument : DocumentBase
    {
        //放置配置的字典
        protected Dictionary<string, Setting> dicPrintSets = new Dictionary<string, Setting>();

        public PostalOrderDocument()
        { }
        public PostalOrderDocument(string defcode)
        {
            this._defCode = defcode;
            getData(defcode);
            loadPrintSettings();

        }

        public PostalOrderDocument(string defcode, bool isPrintBack)
        {
            this.IsPrintBack = isPrintBack;
            this._defCode = defcode;
            getData(defcode);
            loadPrintSettings();
        }

        protected override void OnPrintPage(PrintPageEventArgs e)
        {
            Graphics g = e.Graphics;
            g.PageUnit = GraphicsUnit.Millimeter;
            float fontHeight = g.MeasureString("字体测试高度", base.TitleFont).Height;
            if (this.IsPrintBack)
            {
                if (File.Exists(Application.StartupPath + @"\BackImage\PostalOrder.jpg"))
                {
                    g.DrawImage(Image.FromFile(Application.StartupPath + @"\BackImage\PostalOrder.jpg"), 0, 0, Convert.ToSingle(e.PageBounds.Width * 25.4 / 100),
                   Convert.ToSingle(e.PageBounds.Height * 25.4 / 100));
                }

            }
            if (this.billex != null && this.goodsSum != null)
            {
                g.DrawString(billex.LCBank, base.TitleFont, Brushes.Black, dicPrintSets["LCBank"].PosX, dicPrintSets["LCBank"].PosY);
                g.DrawString(billex.LCno, base.TitleFont, Brushes.Black, dicPrintSets["LCNo"].PosX, dicPrintSets["LCNo"].PosY);
                g.DrawString(billex.LCDate.HasValue ? billex.LCDate.Value.ToString("yyyy年MM月dd日") : "", base.TitleFont, Brushes.Black, dicPrintSets["LCDate"].PosX, dicPrintSets["LCDate"].PosY);
                g.DrawString(billex.Interest, base.TitleFont, Brushes.Black, dicPrintSets["Interest"].PosX, dicPrintSets["Interest"].PosY);
                g.DrawString(billex.BiExNo, base.TitleFont, Brushes.Black, dicPrintSets["PONo"].PosX, dicPrintSets["PONo"].PosY);
                // g.DrawString(billex.Payee, base.TitleFont, Brushes.Black, dicPrintSets["Payee"].PosX, dicPrintSets["Payee"].PosY);
                g.DrawString(billex.LimitDays, base.TitleFont, Brushes.Black, dicPrintSets["PODays"].PosX, dicPrintSets["PODays"].PosY);
                g.DrawString(billex.AmountSay + " " + billex.GreatAmount, base.Font, Brushes.Black, dicPrintSets["Amount2"].PosX, dicPrintSets["Amount2"].PosY);
                g.DrawString(billex.Payer, base.TitleFont, Brushes.Black, dicPrintSets["Consignee"].PosX, dicPrintSets["Consignee"].PosY);
                g.DrawString(billex.Address, base.TitleFont, Brushes.Black, dicPrintSets["Address"].PosX, dicPrintSets["Address"].PosY);
                g.DrawString(billex.Currency + billex.Amount.Value.ToString("F2"), base.TitleFont, Brushes.Black, dicPrintSets["Amount1"].PosX, dicPrintSets["Amount1"].PosY);
            }

        }

        protected void loadPrintSettings()
        {
            XElement printSettings = XElement.Load(AppDomain.CurrentDomain.BaseDirectory + @"\config\PostalOrder.xml");
            IEnumerable<XElement> settings = null;
            settings = (from el in printSettings.Elements("setting")
                        select el);
            if (settings != null && settings.Count() > 0)
            {
                foreach (XElement item in settings)
                {
                    if (!dicPrintSets.ContainsKey(item.Attribute("name").Value))
                    {

                        Setting set = new Setting();
                        set.Name = item.Attribute("name").Value;
                        set.CName = item.Attribute("cname").Value;
                        set.PosX = Convert.ToSingle(item.Attribute("PosX").Value);
                        set.PosY = Convert.ToSingle(item.Attribute("PosY").Value);
                        dicPrintSets.Add(set.Name, set);
                    }
                    else
                    {
                        dicPrintSets[item.Attribute("name").Value].CName = item.Attribute("cname").Value;
                        dicPrintSets[item.Attribute("name").Value].PosX = Convert.ToSingle(item.Attribute("PosX").Value);
                        dicPrintSets[item.Attribute("name").Value].PosY = Convert.ToSingle(item.Attribute("PosY").Value);
                    }
                }
            }
            else
            {

            }

        }
    }
}
